﻿@using Seal.Model;
@{
    MetaTable table = Model;

    //Parameters for this table
    table.Parameters.Add(new Parameter() { Name = "csv_path", Value = "", DisplayName = "CSV File Path", Description = "The CSV file path. The path can contain the '%SEALREPOSITORY%' keyword to specify the repository path.", Enums = new string[] { @"%SEALREPOSITORY%/Databases/Translations.csv" }, UseOnlyEnumValues = false });
    table.Parameters.Add(new Parameter() { Name = "csv_sep", Value = "", DisplayName = "Separator", Description = "The separator character of the CSV field. If empty, the default separator is used.", Enums = new string[] { ",", ";", "\\t" }, UseOnlyEnumValues = false });
    table.Parameters.Add(new Parameter() { Name = "csv_encoding", Value = "UTF8", DisplayName = "File encoding", Description = "The CSV File Encoding.", Enums = new string[] { "Default", "UTF8", "Unicode", "ASCII" }, UseOnlyEnumValues = true });
    table.Parameters.Add(new Parameter() { Name = "csv_vb", BoolValue = true, DisplayName = "Use Microsoft VB Parser", Description = "If true, the Microsoft VB Parser is used instead of the standard parser." });

    table.DefinitionScript = @"@using System.IO
@using System.Data
@{
    MetaTable metaTable = Model;
    var path = metaTable.GetValue(""csv_path"").Replace(Repository.SealRepositoryKeyword, Repository.Instance.RepositoryPath);
    if (metaTable.NoSQLCacheTable != null && File.GetLastWriteTime(path) <= metaTable.LoadDate) {
        metaTable.LogMessage(""Getting table from the load done at {0}."", metaTable.LoadDate);
        metaTable.NoSQLTable = metaTable.NoSQLCacheTable;
    }
    else {
        char? separator = null;
        var sep = metaTable.GetValue(""csv_sep"");
        if (!string.IsNullOrEmpty(sep)) {
            separator = (sep == ""\\t"" ? '\t' : metaTable.GetValue(""csv_sep"")[0]);
        }
        var encoding = System.Text.Encoding.Default;
        if (metaTable.GetValue(""csv_encoding"")==""UTF8"") {
            encoding = System.Text.Encoding.UTF8;
        }
        else if (metaTable.GetValue(""csv_encoding"")==""Unicode"") {
            encoding = System.Text.Encoding.Unicode;
        }
        else if (metaTable.GetValue(""csv_encoding"")==""ASCII"") {
            encoding = System.Text.Encoding.ASCII;
        }

        if (metaTable.GetBoolValue(""csv_vb"", true)) {
            metaTable.NoSQLTable = DataTableLoader.FromCSVVBParser(path, separator, encoding);
        }
        else {
            metaTable.NoSQLTable = DataTableLoader.FromCSV(path, separator, encoding);
        }
    }
}";

    table.LoadScript = @"";
}

